import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

import Home from '@/pages/Home';
import Search from '@/pages/Search';
import Login from '@/pages/Login';
import Register from '@/pages/Register';

let origionPush = VueRouter.prototype.push;
let origionReplace = VueRouter.prototype.replace;
//重新push和replace方法
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        origionPush.call(this, location, resolve, reject)
    } else {
        origionPush.call(this, location, () => { }, () => { })
    }
};
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        origionReplace.call(this, location, resolve, reject)
    } else {
        origionReplace.call(this, location, () => { }, () => { })
    }
};


export default new VueRouter({
    routes: [
        {
            path: '/home',
            component: Home,
            meta: { show: true }
        },
        {
            path: '/search/:keyword?',//制定params参数可传可不传
            name: 'search',
            component: Search,
            meta: { show: true },
            // props:true        //将params参数作为props传给路由组件
            // props:{
            //     a:1,
            //     b:2
            // }
            props: ($route) => {
                return {
                    keyword: $route.params.keyword,
                    k: $route.query.k
                }
            }
        },
        {
            path: '/login',
            component: Login,
            meta: { show: false }
        },
        {
            path: '/register',
            component: Register,
            meta: { show: false }
        },
        {
            path: '*',
            redirect: '/home'
        }


    ]
})